var displayMales = true;
var displayFemales = true;
var displayFriendsOnly = false;
var displaySinglesOnly = false;

// Update the actual stream.
function updateStream(){
    showStreamLoader();
    setTimeout(function(){
        var date = '';
        
        // If the filter was date, pass through the date.
        if(activeFilter === 'date'){
            date = $('#filterDateInput').val();
        }
        
        $.ajax({
            data: {
                'filter': activeFilter,
                'date': date,
                'limit': limit,
                'displayMales': displayMales,
                'displayFemales': displayFemales,
                'displayFriendsOnly': displayFriendsOnly,
                'displaySinglesOnly': displaySinglesOnly,
                'companyId': companyId
            },
            url: 'ajaj/readCompanyStream',
            type: 'POST',
            dataType: 'json',
            success: function(results){
                $('#tileStream').empty();
                
                if(results){
                    populateStream(results);
                }
                
                removeStreamLoader();
            }
        });
        
    }, streamTimerInterval);
}

  function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
  }

// Populate function that will actually fill in the stream.
function populateStream(results){
    var indexCount = 0;
    
    $.each(results, function(i, a){
        
        console.log(a);
        var li = '<li class="streamTile">';
            li += '<table>';
                li += '<tr><td><img src="https://graph.facebook.com/' + a.userId + '/picture" alt="' + ucwords(a.name) + '" /><td></tr>';
                li += '<tr><td class="nameTitle"><a href="profile/user/' + a.userId + '">' + ucwords(a.firstName) + '</a></td></tr>';
            li += '</table>';
            li += '<div class="tileInformation">';
                li += '<table>';
                    li += '<tr><td class="title"><a href="profile/user/' + a.userId + '">' + ucwords(a.name) + '</a> (' + a.age + ')<td></tr>';
                    
                    var time = '';
                    
                    if(a.time !== null){
                        time = 'om ' + a.time.substr(0, 5);
                    }
                    
                    var message = '';
                    var time = '';
                    var till = '';
                 
                        
                        if(a.messageHelper[1] !== undefined){
                            message = ucwords(a.messageHelper[1]);

                        } else {
                            message = ucwords(a.messageHelper[2]);
                        }
                        
                        if(a.time !== null){
                            time += 'Om ' + a.time.substr(0, 5);
                        }

                        if(a.endDate !== null){
                            if(a.messageHelper[3] !== undefined){
                                till += 'Tot ' + a.messageHelper[3];
                            } else if(a.messageHelper[4] !== undefined){
                                till += 'Tot ' + ucwords(a.messageHelper[4]);

                                if(a.endTime !== null){
                                    till += ' om ' + a.endTime.substr(0, 5);
                                }
                            } else {
                                if(a.endTime !== null){
                                    till += 'tot ' + a.endTime.substr(0, 5);
                                }
                            }
                        }
                        
                        li += '<tr><td>' + message + '<td></tr>';
                        li += '<tr><td>' + time + '<td></tr>';
                        li += '<tr><td>' + till + '<td></tr>';
                    
                    // li += '<tr><td class="title">' + ucwords(a.time.substr(0, 5)) + '<td></tr>';
                li += '</table>';
            li += '</div>';
            li += '<div class="clear"></div>';
        li += '</li>';
        li = $(li);
        li.appendTo('#tileStream');
    });
    
    $('.streamTile').click(function(){
        if($(this).find('.tileInformation').is(':hidden')){
            $(this).find('.tileInformation').show();
        } else {
            $(this).find('.tileInformation').hide();
        }
    });
}

$(document).ready(function(){
    $('#displayMales').click(function(){
        if(!displayMales){
            displayMales = true;
        } else {
            displayMales = false;
        }
    })
    
    $('#displayFemales').click(function(){
       if(!displayFemales){
           displayFemales = true;
       } else {
           displayFemales = false;
       }
    });
    
    $('#displayFriendsOnly').click(function(){
       if(!displayFriendsOnly){
           displayFriendsOnly = true;
       } else {
           displayFriendsOnly = false;
       }
    });
    
    $('#displaySinglesOnly').click(function(){
        if(!displaySinglesOnly){
            displaySinglesOnly = true;
        } else {
            displaySinglesOnly = false;
        }
    });
    
	$('#extraFilterList li').click(function(){
		if(!$(this).find('.checkbox').hasClass('checked')){
			$(this).find('.checkbox').addClass('checked');
		} else {
			$(this).find('.checkbox').removeClass('checked');
		}
		
		updateStream();
	});
    
    $('#displayMales').find('.checkbox').addClass('checked');
    $('#displayFemales').find('.checkbox').addClass('checked');
});